import { _decorator, assetManager, Component, ImageAsset, isValid, Label, Node, Sprite, SpriteFrame, Texture2D } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('WorldRankItem')
export class WorldRankItem extends Component {

    @property({ type: Label, tooltip: '名次' })
    lbRank: Label = null;

    @property({ type: Sprite, tooltip: '头像' })
    spAvatar: Sprite = null;

    @property({ type: Label, tooltip: '昵称' })
    lbNickname: Label = null;

    @property({ type: Label, tooltip: '分数' })
    lbScore: Label = null;

    @property({ type: [Node], tooltip: '奖章' })
    model: Node[] = [];
    

    init(rankData: { rank: number, nickname: string, score: number; }) {
        
        if (this.lbRank) this.lbRank.string = rankData.rank.toString();
        if (this.lbNickname) this.lbNickname.string = rankData.nickname;
        if (this.lbScore) this.lbScore.string = rankData.score.toString();

        assetManager.loadRemote('http://localhost/avatar.png', (err: Error, asset: ImageAsset) => {
            if (err || !isValid(this.node, true)) return;
            const texture2d = new Texture2D();
            texture2d.image = asset;
            const spriteFrame = new SpriteFrame();
            spriteFrame.texture = texture2d;
            this.spAvatar.spriteFrame = spriteFrame;
        });

        if (rankData.rank <= 3 && rankData.rank < this.model.length) {
            this.model[rankData.rank - 1].active = true;
        } else {
            if (this.lbRank) this.lbRank.node.active = true;
        }
    }


}

